package com.project.common.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @Description: Redis1
 * @Author: zhuchi
 * @CreateDate: 2020/9/11 17:49
 * @Version: 1.0
 */
@Configuration
@EnableCaching
public class RedisConfig1 extends RedisConfigBase {

    @Value("${spring.redis.database}")
    private int dbIndex;

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.timeout}")
    private int timeout;

    /**
     * 配置redis连接工厂
     */
    @Bean
    public RedisConnectionFactory redisConnectionFactory1() {
        return createJedisConnectionFactory(dbIndex, host, port, password, timeout);
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="redisTemplate1") 方式注入
     */
    @Bean(name = "redisTemplate1")
    public RedisTemplate redisTemplate() {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory1());
        super.setSerializer(template);
        template.afterPropertiesSet();
        return template;
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="redisStringTemplate1") 方式注入
     */
    @Bean(name = "stringRedisTemplate1")
    public StringRedisTemplate rtringRedisTemplate() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory1());
        super.setSerializer(template);
        template.afterPropertiesSet();
        return template;
    }

}
